package com.longdaji.decoration.ui.activitiesOfMine.allOrder;

import com.longdaji.decoration.Constants;
import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.bean.Orders;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.data.network.HttpResponse;
import com.longdaji.decoration.utils.LogUtil;
import com.longdaji.decoration.utils.RxUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Kanade on 2018/4/19 0019.
 */

public class AllOrderPresenter extends RxPresenter<AllOrderContract.IView> implements AllOrderContract.IPresenter {
    private static final String TAG = "AllOrderPresenter";
    private DataManager mDataManager;
    private List<Orders.OrdersBean.OrderBean> allOrderList = new ArrayList<>();

    private String userid;

    @Inject
    public AllOrderPresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
    }

    @Override
    public void initData() {
        userid = Account.getInstance().getUserid();
        doGetOrder();
    }

    private void doGetOrder() {
        addSubscribe(mDataManager.doGetOrderApi(userid, null, null)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<Orders>handleResult())
                .subscribeWith(new CommonSubscriber<Orders>() {
                    @Override
                    public void onSuccess(final Orders orders) {
                        LogUtil.d("succeed : doGetOrder ");
                        mView.showGetOrderSucceed(orders.getOrders().getOrder());
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d("failed : " + msg);
                    }
                }));
    }

    @Override
    public void onCancelOrderPaymentClick(String orderNo) {
        addSubscribe(mDataManager.doCancelOrderApi(userid, orderNo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        LogUtil.d("succeed : onCancelOrderClick ");
                        // TODO: 2018/4/16 0016  set order status to 4,and refresh pending payment order list
                        //这里应该是取消订单后后端自动设置order status为4了，所以我这里只要重新获取一下待付款订单就好
                        doGetOrder();
                        mView.showCancelOrderSucceed();
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d("failed : " + msg);

                    }
                }));
    }

    @Override
    public void onCancelOrderDeliveryClick(final String orderNo) {
        addSubscribe(mDataManager.doCancelOrderApi(userid, orderNo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        LogUtil.d("succeed : onCancelOrderClick ");
                        // TODO: 2018/4/17 0017 refresh pending delivery order list
                        doRefund(userid, orderNo);
                        doGetOrder();
                        mView.showCancelOrderSucceed();
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d("failed : " + msg);

                    }
                }));
    }

    private void doRefund(String userid, String orderNo) {
        addSubscribe(mDataManager.doRefundApi(userid, orderNo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        LogUtil.d("succeed : doRefundApi ");
                        mView.showRefundSucceed();
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d("failed : " + msg);

                    }
                }));
    }

    @Override
    public void onConfirmOrderClick(String orderNo) {
        addSubscribe(mDataManager.doReceivingGoodsApi(Account.getInstance().getUserid(), orderNo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        LogUtil.d(TAG, "doReceivingGoodsApi");
                        doGetOrder();
                    }

                    @Override
                    public void onFailed(String msg) {

                    }
                })
        );
    }
}
